package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.CustomException;
import com.example.common.Result;
import com.example.entity.Department;
import com.example.entity.DepartmentPosition;
import com.example.service.DPService;
import com.example.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Api(tags = {"部门接口列表"})
@RestController
@RequestMapping("/department")
@Slf4j
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DPService dpService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param manager
     * @return
     */
    @ApiOperation("部门查询")
    @GetMapping("/list")
    public Result<?> list(int page, int pageSize, String name, String manager) {

        Page<Department> departmentPage = new Page(page, pageSize);

        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Department::getName, name);
        departmentLambdaQueryWrapper.like(StringUtils.isNotEmpty(manager), Department::getManager, manager);
        departmentService.page(departmentPage, departmentLambdaQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("total", departmentPage.getTotal());
        map.put("rows", departmentPage.getRecords());
        return Result.success(map);

    }

    /**
     * 添加新部门 ，需要和od表进行联动
     *
     * @param
     * @return
     */
    @ApiOperation("添加新部门")
    @PostMapping
    public Result<?> addDepartment(@RequestBody Department department) {
        log.info("{}", department);
        long orderId = IdWorker.getId();
        department.setId(orderId);
        departmentService.save(department);
//        departmentService.save(departmentDto);
        return Result.success("添加部门成功");
    }

    /**
     * 修改部门，和od表联动
     *
     * @param
     * @return
     */
    @ApiOperation("修改部门")
    @PutMapping
    public Result<?> updateDepartment(@RequestBody Department department) {

        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.eq(Department::getId, department.getId());
        Department one = departmentService.getOne(departmentLambdaQueryWrapper);

        if (one.getStatus() != department.getStatus() && one.getStatus() == 1) {
            LambdaQueryWrapper<DepartmentPosition> departmentPositionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            departmentPositionLambdaQueryWrapper.eq(DepartmentPosition::getDepartmentId, department.getId());
            long count = dpService.count(departmentPositionLambdaQueryWrapper);
            if (count > 0) {
                return Result.fail("该部门正在使用，不能修改其状态");
            }
        }

        departmentService.updateById(department);
        return Result.success("修改部门成功");
    }

    /**
     * 查找部门返回修改界面，和od联动
     *
     * @param id
     * @return
     */

    @ApiOperation("返回到修改界面的查询部门")
    @GetMapping("/{id}")
    public Result<Department> getById(@PathVariable Long id) {
        return Result.success(departmentService.getById(id));
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */

    @ApiOperation("删除部门")
    @DeleteMapping("/{id}")
    public Result<?> deleteDepartment(@PathVariable Long id) {

        LambdaQueryWrapper<DepartmentPosition> departmentPositionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentPositionLambdaQueryWrapper.eq(DepartmentPosition::getDepartmentId, id);
        long count = dpService.count(departmentPositionLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("该部门已关联表，不能删除");
        } else {
            departmentService.removeById(id);
            return Result.success("删除部门成功");
        }
    }

    /**
     * 部分需要下拉选择部门的功能
     *
     * @param department
     * @return
     */

    @ApiOperation("修改界面的部门数据")
    @GetMapping("/name")
    public Result<?> getDepartmentName(Department department) {
        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.eq(department.getStatus() != null, Department::getStatus, department.getStatus());

        List<Department> list = departmentService.list(departmentLambdaQueryWrapper);

        log.info("{}", list);
        return Result.success(list);
    }
}
